package com.example.demo.controller;

import com.example.demo.config.DoctorConfig;
import com.example.demo.config.UserConfig;
import com.example.demo.model.AnnotationDemo;
import com.example.demo.service.AnnoService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Map;


@RestController
@RequestMapping("/api/annotation")
public class AnnotationController {


    @Autowired
    private UserConfig bean;

    @Autowired
    private DoctorConfig doctorConfig;


    @Autowired
    private AnnoService annoService;

    @Autowired
    public AnnotationController(ConfigurableApplicationContext context) {
        var bean  = context.getBean(UserConfig.class);
        System.out.println("user score: " + bean.getUserScore());
    }
    @PostMapping("/body")
    public ResponseEntity<String> getBody(@RequestBody String body){

        return ResponseEntity.ok( body);
    }

    @GetMapping("/config")
    public ResponseEntity<Map<String, String>> getConfig(){

        annoService.printUsers();
        String config = bean.getUserScore();

        System.out.println("user score: " + config);
        System.out.println("bean score: " + bean.getUserMap());
        Map<String, String> db = annoService.getDbConfig();

        annoService.printUserDetails();
        System.out.println("doctors config: " + annoService.getMembers());

        System.out.println("db config: " + db.get("url"));
        System.out.println("db config: " + db.get("port"));
        return ResponseEntity.ok( db);
    }

    @PostMapping("/notnull")
    public String notNullValid(@Valid @RequestBody AnnotationDemo demo){
        System.out.println(demo);
        return "success";

    }


    // transactional 的注解


}
